<?php

defined('TD_TRUE') or die(header('HTTP/1.1 404 NOT FOUND'));

/**
 * 读模板页进行替换后写入到cache页里
 *
 * @param string $tplfile ：模板文件名
 * @param string $objfile ：cache文件名
 * @return 
 */
function parse_template($file,$path) {
	global $_C;
    $Lpra = '\{' ;
    $Rpra = '\}' ;
	$Lbra = '\[' ;
    $Rbra = '\]' ;
	//read   
	$tplfile = TD_ROOT.'./views/'.$path.$file.$_C['tpl_ex'];   
	$objfile = TD_ROOT.'./cache/'.$path.$file.'.php';
	$template = sreadfile($tplfile);
	if(empty($template)) {
		exit("Template file : $tplfile Not found or have no access!");
	}

	//开始处理,禁用PHP的支持
	$template = str_replace('<?', '{LTAG}', $template);
	$template = str_replace('?>', '{RTAG}', $template);
	$template = str_replace('<%', '{ASPLTAG}', $template);
	$template = str_replace('%>', '{ASPRTAG}', $template);
	$template = str_replace('{LTAG}','<?php echo \'<?\'; ?>', $template);
	$template = str_replace('{RTAG}','<?php echo \'?>\';?>', $template);
	$template = str_replace('{ASPLTAG}','<?php echo \'<%\'; ?>', $template);
	$template = str_replace('{ASPRTAG}','<?php echo \'%>\'; ?>', $template);
	

  //pre标签 预存保留原格式的代码
  preg_match_all("/(<!--)?\s*".$Lpra."pre".$Rpra."\s*(-->)?(.*?)(<!--)?\s*".$Lpra."\/pre".$Rpra."\s*(-->)?/is", $template,$pre_old);	
    //模板调用
	$template = preg_replace("/".$Lpra."\s*include\s+([a-z0-9_\/]+)\s*".$Rpra."/ie", "readtemplate('\\1')", $template);

	//时间处理
	$template = preg_replace("/".$Lpra."\s*date\((.+?)\)\s*".$Rpra."/is", "<?php echo dateformat('\\1'); ?>", $template);
	//头像处理
	$template = preg_replace("/".$Lpra."\s*avatar\((.+?)\)\s*".$Rpra."/is", "<?php echo avatar('\\1'); ?>", $template);

	//开始处理
	
	//变量
	//$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[\])*)";
	//变量
	$var_regexp = "(([a-zA-Z_\x7f-\xff\$][a-zA-Z0-9_\x7f-\xff\$]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
	//字符串
	$str_regexp = "([\'\"]+(.*?)[\'\"]+)";
	//数字
	$num_regexp = "([\d]+)";
	//常量
	$const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";
		
	//系统变量
	$template = preg_replace("/".$Lbra."\s*[sS]{1,1}\s*\:\s*".$var_regexp."\s*".$Rbra."/s", '<?php echo $_SYSVAR[\''."\\1".'\'];?>', $template);
	//用户自定义的变量
	$template = preg_replace("/".$Lbra."\s*[uU]{1,1}\s*\:\s*".$var_regexp."\s*".$Rbra."/s", '<?php echo $_USERVAR[\''."\\1".'\'];?>', $template);
	
	//普通函数的解析
	$template = preg_replace("/".$Lpra."\s*([a-zA-Z0-9_\x7f-\xff]+\([a-zA-Z0-9_\-\.\"\'\[\]\&\,\$\x7f-\xff]*\))\s*".$Rpra."/is","<?php echo \\1 ;?>", $template);
	
	//支持定界符的变量
	$template = preg_replace("/".$Lpra."\s*(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\s*".$Rpra."/s", "<?php echo \\1;?>", $template);
	
    //逻辑if语句
	$template = preg_replace("/".$Lpra."\s*if\s+(.+?)\s*".$Rpra."/ies", "stripvtags('<?php if(\\1) { ?>','')", $template);
	$template = preg_replace("/".$Lpra."\s*\/if\s*".$Rpra."/is", "<?php } ?>", $template);
	$template = preg_replace("/".$Lpra."\s*elseif\s+(.+?)\s*".$Rpra."/ies", "stripvtags('<?php } elseif(\\1) { ?>,'')", $template);
	$template = preg_replace("/".$Lpra."\s*else\s*".$Rpra."/is", "<?php } else { ?>", $template);
	//循环 loop 语句
	$template = preg_replace("/".$Lpra."\s*loop\s+(\S+)\s+(\S+)\s*".$Rpra."/ies", "stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','')", $template);
    $template = preg_replace("/".$Lpra."\s*\/loop\s*".$Rpra."/is", "<?php } } ?>", $template);
	$template = preg_replace("/".$Lpra."\s*loop\s+(\S+)\s+(\S+)\s+(\S+)\s*".$Rpra."/ies", "stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>','')", $template);	
    $template = preg_replace("/".$Lpra."\s*\/loop\s*".$Rpra."/is", "<?php } } ?>", $template);	
	
	//模板特殊变量的支持
	$sftpl = $_C['siteurl'].'/views/'.$path.'_SFTPL_';
	$template = str_replace("_SFTPL_", $sftpl , $template);

	//换行
	$template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
   //del标签 删除注释
   $template = preg_replace("/(<!--)?\s*".$Lpra."del".$Rpra."\s*(-->)?(.*?)(<!--)?\s*".$Lpra."\/del".$Rpra."\s*(-->)?/is", '', $template);
  //还原 pre 里面的内容
  preg_match_all("/(<!--)?\s*".$Lpra."pre".$Rpra."\s*(-->)?(.*?)(<!--)?\s*".$Lpra."\/pre".$Rpra."\s*(-->)?/is", $template,$pre_new);
	$template = str_replace($pre_new[0], $pre_old[3], $template);
	//write
	if(!file_exists(TD_ROOT.'./cache/'.$path)){
	if(!mkdir(TD_ROOT.'./cache/'.$path))
	   exit("File: can not create folder $path!"); 
	}
	if(!swritefile($objfile, $template)) {
		exit("File: $objfile can not be write!");
	}
}

function addquote($var) {
	return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}

function striptagquotes($expr) {
	$expr = preg_replace("/\<\?php echo(\\\$.+?)\?\>/s", "\\1", $expr);
	$expr = str_replace("\\\"", "\"", preg_replace("/\[\'([a-zA-Z0-9_\-\.\x7f-\xff]+)\'\]/s", "[\\1]", $expr));
	return $expr;
}

function evaltags($php) {
	global $_G;

	$_G['i']++;
	$search = "<!--EVAL_TAG_{$_G['i']}-->";
	$_G['block_search'][$_G['i']] = $search;
	$_G['block_replace'][$_G['i']] = "<?php ".stripvtags($php)." ?>";
	
	return $search;
}

function blocktags($parameter) {
	global $_G;

	$_G['i']++;
	$search = "<!--BLOCK_TAG_{$_G['i']}-->";
	$_G['block_search'][$_G['i']] = $search;
	$_G['block_replace'][$_G['i']] = "<?php block(\"$parameter\"); ?>";
	return $search;
}

function adtags($pagetype) {
	global $_G;

	$_G['i']++;
	$search = "<!--AD_TAG_{$_G['i']}-->";
	$_G['block_search'][$_G['i']] = $search;
	$_G['block_replace'][$_G['i']] = "<?php adshow('$pagetype'); ?>";
	return $search;
}

function datetags($parameter) {
	global $_G;

	$_G['i']++;
	$search = "<!--DATE_TAG_{$_G['i']}-->";
	$_G['block_search'][$_G['i']] = $search;
	$_G['block_replace'][$_G['i']] = "<?php echo sgmdate($parameter); ?>";
	return $search;
}



function stripvtags($expr, $statement='') {
	$expr = str_replace("\\\"", "\"", preg_replace("/\<\?php echo\s*(\\\$.+?);\s*\?\>/s", "\\1", $expr));
	$statement = str_replace("\\\"", "\"", $statement);
	return $expr.$statement;
}

function readtemplate($name) {
	global $_G, $_C;
	
	$tpl = strexists($name,'/')?$name:TPL_ROOT.'/'.$name;
	
	$_G['sub_tpls'][] = $tpl;
	$file = TD_ROOT.'./'.$tpl.$_C['tpl_ex'];
	$content = sreadfile($file);
	$template = preg_replace("/".$Lpra."\s*include\s+([a-z0-9_\/]+)\s*".$Rpra."/ie", "readtemplate('\\1')", $template);
	return $content;
}
function vartostring($var){
	return $var;
}
?>